﻿
namespace SistemaLogistica.Windows.Panels
{
    using System;
    using System.Reflection;
    using System.Windows.Forms;
    using SistemaLogistica.Business.Entities;
    using SistemaLogistica.Windows.Helpers;
    using WeifenLuo.WinFormsUI.Docking;

    public partial class FrmDocument : DockContent
    {
        public UserControl Contenido { get; set; }

        public static event EventHandler<EventArgs> ShowedWindow;

        public static void OnShowedWindow(UserControl userControl)
        {
            EventHandler<EventArgs> handler = ShowedWindow;
            if (handler != null)
            {
                handler(userControl, new EventArgs());
            }
        }

        public FrmDocument(Formulario formulario)
        {
            InitializeComponent();

            this.TabText = formulario.Nombre;
            this.Text = formulario.ClassName;

            Assembly asm = Assembly.GetExecutingAssembly();
            Contenido = (UserControl)asm.CreateInstance(formulario.Menus.Namespace + "." + formulario.ClassName);
            if (Contenido != null)
            {
                Contenido.Name = "Contenido";
                Contenido.Tag = formulario.IdFormulario;
                Contenido.Dock = DockStyle.Fill;
                this.Controls.Add(Contenido);
            }
        }

        private void FrmDocument_Load(object sender, EventArgs e)
        {
            if (DesignMode == false)
            {
                ObservarSujeto();
            }
        }

        private void FrmDocument_Enter(object sender, EventArgs e)
        {
            try
            {
                if (Contenido != null)
                {
                    OnShowedWindow(Contenido);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }  

        private void CloseTab_Click(object sender, EventArgs e)
        {
            Observador observador = Contenido as Observador;
            if (observador != null)
            {
                foreach (IDockContent content in DockPanel.Documents)
                {
                    Sujeto sujeto = content.DockHandler.Form.Controls["Contenido"] as Sujeto;
                    if (sujeto != null) 
                    {
                        sujeto.Remover(observador);
                    }
                }
            }
            this.Close();
        }

        private void ObservarSujeto()
        {
            Observador observador = Contenido as Observador;
            Sujeto sujeto = Contenido as Sujeto;

            if (observador != null) //Registrar este Formulario(Observador) a sus Sujetos
            {
                switch (this.Text)
                {
                    case "CwfBienes":
                    case "CwfBienPatrimonial":
                        foreach (IDockContent content in DockPanel.Documents)
                        {
                            Form frm = content.DockHandler.Form;
                            switch (frm.Text)
                            {
                                case "CwfUnidadMedida":
                                    (frm.Controls["Contenido"] as Sujeto).Registrar(observador);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case "CwfFactura":
                    case "CwfNotaIngreso":
                    case "CwfAlquilerVehiculo":
                        foreach (IDockContent content in DockPanel.Documents)
                        {
                            Form frm = content.DockHandler.Form;
                            switch (frm.Text)
                            {
                                case "CwfProveedor":
                                    (frm.Controls["Contenido"] as Sujeto).Registrar(observador);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case "CwfOrdenCompra":
                        foreach (IDockContent content in DockPanel.Documents)
                        {
                            Form frm = content.DockHandler.Form;
                            switch (frm.Text)
                            {
                                case "CwfProveedor":
                                case "CwfUnidadMedida":
                                    (frm.Controls["Contenido"] as Sujeto).Registrar(observador);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case "CwfVenta":
                        foreach (IDockContent content in DockPanel.Documents)
                        {
                            Form frm = content.DockHandler.Form;
                            switch (frm.Text)
                            {
                                case "CwfClientesNatural":
                                case "CwfClienteJuridico":
                                    (frm.Controls["Contenido"] as Sujeto).Registrar(observador);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case "CwfMantenimiento":
                        foreach (IDockContent content in DockPanel.Documents)
                        {
                            Form frm = content.DockHandler.Form;
                            switch (frm.Text)
                            {
                                case "CwfVehiculo":
                                case "CwfVehiculoAlquilado":
                                    (frm.Controls["Contenido"] as Sujeto).Registrar(observador);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case "CwfEntregaVehiculo":
                    case "CwfEntregaTelefono":
                        foreach (IDockContent content in DockPanel.Documents)
                        {
                            Form frm = content.DockHandler.Form;
                            switch (frm.Text)
                            {
                                case "CwfNotaSalida":
                                    (frm.Controls["Contenido"] as Sujeto).Registrar(observador);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
            if (sujeto != null) //Registrar todos los Observadores de este Formulario(Sujeto)
            {
                switch (this.Text)
                {
                    case "CwfProveedor":
                        foreach (IDockContent content in DockPanel.Documents)
                        {
                            Form frm = content.DockHandler.Form;
                            switch (frm.Text)
                            {
                                case "CwfFactura":
                                case "CwfOrdenCompra":
                                case "CwfNotaIngreso":
                                case "CwfAlquilerVehiculo":
                                    sujeto.Registrar(frm.Controls["Contenido"] as Observador);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case "CwfUnidadMedida":
                        foreach (IDockContent content in DockPanel.Documents)
                        {
                            Form frm = content.DockHandler.Form;
                            switch (frm.Text)
                            {
                                case "CwfBienes":
                                case "CwfOrdenCompra":
                                case "CwfBienPatrimonial":
                                    sujeto.Registrar(frm.Controls["Contenido"] as Observador);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case "CwfClientesNatural":
                    case "CwfClienteJuridico":
                        foreach (IDockContent content in DockPanel.Documents)
                        {
                            Form frm = content.DockHandler.Form;
                            switch (frm.Text)
                            {
                                case "CwfVenta":
                                    sujeto.Registrar(frm.Controls["Contenido"] as Observador);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case "CwfVehiculo":
                    case "CwfVehiculoAlquilado":
                        foreach (IDockContent content in DockPanel.Documents)
                        {
                            Form frm = content.DockHandler.Form;
                            switch (frm.Text)
                            {
                                case "CwfMantenimiento":
                                    sujeto.Registrar(frm.Controls["Contenido"] as Observador);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case "CwfNotaSalida":
                        foreach (IDockContent content in DockPanel.Documents)
                        {
                            Form frm = content.DockHandler.Form;
                            switch (frm.Text)
                            {
                                case "CwfEntregaVehiculo":
                                case "CwfEntregaTelefono":
                                    sujeto.Registrar(frm.Controls["Contenido"] as Observador);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
